package com.wep.fence.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wep.fence.constant.Constants;
import lombok.Data;
import lombok.ToString;

import java.util.Collections;

/**
 * @param <T>
 * @author wep
 */
@Data
@ToString(callSuper = true)
public class PageR<T> extends R {
    private Long count;

    public static <T> PageR<T> ok(Long count, T data) {
        PageR r = new PageR();
        r.setCode(Constants.SUCCESS);
        r.setMsg("查询成功");
        r.setData(data);
        r.setCount(count);
        return r;
    }

    public static <T> PageR<T> ok(IPage<T> page) {
        PageR r = new PageR();
        r.setCode(Constants.SUCCESS);
        r.setMsg("查询成功");
        if (page.getTotal() <= 0) {
            r.setCount(0L);
            return r;
        }
        r.setData(page.getRecords());
        r.setCount(page.getTotal());
        return r;
    }

    public static <T> PageR<T> noData() {
        PageR r = new PageR();
        r.setCode(0);
        r.setMsg("查询成功");
        r.setData(Collections.emptyList());
        r.setCount(0L);
        return r;
    }
}
